<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *
 */
class Layout {

    function render() {

        global $OUT;
        $CI = & get_instance();
        $output = $CI->output->get_output();
        if (!isset($CI->layout)) {
            $CI->layout = "default";
        }
        if ($CI->layout != false) {
            if (!preg_match('/(.+).php$/', $CI->layout)) {
                $CI->layout .= '.php';
            }

            $requested = BASEPATH . '../application/views/layouts/' . $CI->layout;
            $default = BASEPATH . '../application/views/layouts/default.php';

            if (file_exists($requested)) {
                $layout = $CI->load->file($requested, true);
            } else {
                $layout = $CI->load->file($default, true);
            }

            $view = str_replace("{content}", $output, $layout);
            $view = str_replace("{title}", $CI->title, $view);
           
        } else {
            $view = $output;
        }
        $OUT->_display($view);
    }

}
